<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<title>自建类生成敌人</title>
<script src="phaser.min.js">
</script>
</head>

<body>


<script>
/**
*几个游荡的敌人
*1. 在固定几个点生成敌人
*2. 敌人在一定范围内来回移动
**/
var config={
	type:Phaser.AUTO,
	width:800,
	height:600,
	scene:{
		preload:preload,
		create:create,
		update:update
	},
	physics:{
		default:"arcade",
		arcade:{
			debug:false,
			gravity:{y:0}
		
		}
	}
};
var game;
var skeletons = [];
//设定固定点刷怪的坐标
var fixedPoints={
	point1:{offset: 0, x: -2, y: 0, opposite: 'point11'},
	point11:{offset: 45, x: 2, y: 0, opposite: 'point1'},
	
	point2:{ offset: 160, x: 2, y: 1, opposite: 'point22' },
	point22:{offset: 160, x: -2, y: -1, opposite: 'point2' },
	
	point3:{ offset: 224, x: -2, y: 1, opposite: 'point33' },
	point33:{offset: 224, x: 2, y: -1, opposite: 'point3' }
};
game=new Phaser.Game(config);

function  preload(){	
	this.load.image("bg","images/timg.jpg");
	this.load.spritesheet('skeleton', 
		'images/h1.png',
		{ frameWidth: 80, frameHeight: 80 }
	);
	
}
function  create(){

	platforms = this.physics.add.staticGroup();
	platforms.create(400, 300, 'bg');
	
	//所谓的敌人类,功能左右移动，随机生成敌人
	var Skeleton = new Phaser.Class({
		Extends: Phaser.GameObjects.Image,
		initialize:function skeleton(scene,x,y,direction,distance){
			//初始化敌人位置
		    this.startX = x;
            this.startY = y;
            this.distance = distance;
			this.direction = fixedPoints[direction];
			
			
			Phaser.GameObjects.Image.call(this, scene, x, y, 'skeleton', this.direction.offset);
            this.depth = y + 64;
			console.log("this depth:"+this.depth);
		},
		update:function(){
		
			this.x += this.direction.x *0.15;

			if (this.direction.y !== 0)
			{
				this.y += this.direction.y * 0.16;
				this.depth = this.y + 64;
			}
			//来回游荡
			if (Phaser.Math.Distance.Between(this.startX, this.startY, this.x, this.y) >= this.distance)
			{
				this.direction = directions[this.direction.opposite];
				this.f = 0;
				this.frame = this.texture.get(this.direction.offset + this.f);
				this.startX = this.x;
				this.startY = this.y;
			}
		}
	});
	skeletons.push(this.add.existing(new Skeleton(this, 200, 200, 'point1', 200)));
	skeletons.push(this.add.existing(new Skeleton(this, 400, 100, 'point2', 100)));
	skeletons.push(this.add.existing(new Skeleton(this, 300, 50, 'point3', 100)));

}

function update(){

	skeletons.forEach(function (skeleton) {
		skeleton.update();
	});


}



</script>
</body>
</html>